package com.chungechunyemen.post.dao;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chungechunyemen.post.po.Replies;

/**
 * A data access object (DAO) providing persistence and search support for
 * Replies entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.chungechunyemen.post.po.Replies
 * @author MyEclipse Persistence Tools
 */

public class RepliesDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(RepliesDAO.class);
	// property constants
	public static final String POSITION = "position";
	public static final String TITLE = "title";
	public static final String CONTEXT = "context";
	public static final String IMAGE_URL = "imageUrl";
	public static final String IP = "ip";
	public static final String IPORUN = "iporun";
	public static final String STATE = "state";

	protected void initDao() {
		// do nothing
	}

	public void save(Replies transientInstance) {
		log.debug("saving Replies instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Replies persistentInstance) {
		log.debug("deleting Replies instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Replies findById(java.lang.Integer id) {
		log.debug("getting Replies instance with id: " + id);
		try {
			Replies instance = (Replies) getHibernateTemplate().get(
					"com.chungechunyemen.post.po.Replies", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Replies instance) {
		log.debug("finding Replies instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Replies instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Replies as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPosition(Object position) {
		return findByProperty(POSITION, position);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByContext(Object context) {
		return findByProperty(CONTEXT, context);
	}

	public List findByImageUrl(Object imageUrl) {
		return findByProperty(IMAGE_URL, imageUrl);
	}

	public List findByIp(Object ip) {
		return findByProperty(IP, ip);
	}

	public List findByIporun(Object iporun) {
		return findByProperty(IPORUN, iporun);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findAll() {
		log.debug("finding all Replies instances");
		try {
			String queryString = "from Replies";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Replies merge(Replies detachedInstance) {
		log.debug("merging Replies instance");
		try {
			Replies result = (Replies) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Replies instance) {
		log.debug("attaching dirty Replies instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Replies instance) {
		log.debug("attaching clean Replies instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static RepliesDAO getFromApplicationContext(ApplicationContext ctx) {
		return (RepliesDAO) ctx.getBean("RepliesDAO");
	}

	// 按属性值分页查询
	public List findByPropertyAndPage(String propertyName, final Object value,
			final int offset, final int pageSize) {
		final String hql = "from Replies as model where model." + propertyName
				+ "= ?";
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				List result = session.createQuery(hql).setParameter(0, value)
						.setFirstResult(offset).setMaxResults(pageSize).list();
				return result;
			}
		});
		return list;
	}

	// 按属性值查询总数
	public int findCountByProperty(String propertyName, final Object value) {
		final String queryString = "select count(*) from Replies as model where model."
				+ propertyName + "= ?";
		List list = getHibernateTemplate().find(queryString, value);
		if (list == null || list.size() == 0) {
			return 0;
		}
		return ((Long) list.get(0)).intValue();
	}

	// 获取某贴最高楼数
	public int getMaxPostionByTopicId(int topicId) {
		final String hql = "select max(model.position) from Replies as model where model.topic.topicId = ?";
		List list = getHibernateTemplate().find(hql, topicId);
		if (list == null || list.size() == 0 || list.get(0) == null) {
			return 0;
		}
		return ((Integer) list.get(0)).intValue();
	}
}